import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Represent a PushButton when pressed and when not pressed.
 * 
 * @author Christian Pérez Zavalija
 * @version 25-Jun-2013
 */
public class PushButton extends Device
{
    private GreenfootImage []imgPushbutton;
    
    /**
     *  Load the images in an array and displays the Push-Button unPush and called to the constructor
     *  of the super class.
     */
    public PushButton()
    {
        super(2,12,44);
        pins[1].charge=1;
        imgPushbutton=new GreenfootImage[2];
        imgPushbutton[0]= new GreenfootImage("Push-Button unPush.png");
        imgPushbutton[1]= new GreenfootImage("Push-Button push.png");
        setImage(imgPushbutton[0]);
    }
    
    /**
     * The image is scale in 100%
     */
    public void scale()
    {
        imgPushbutton[0].scale(imgPushbutton[0].getWidth(),imgPushbutton[0].getHeight());
    }
    
    /**
     * if you touch one of the pins with the dump, the pins are removed.
     */
    public void eliminate()
    {
        removeTouching(Pin.class);
    }
    
    /**
     * Changes the image to Push-Button push.
     */
    public void Push()
    {
        setImage(imgPushbutton[1]);
        Greenfoot.delay(8);
    }
    
    /**
     * Changes the image to Push-Button unPush.
     */
    public void UnPush()
    {
        setImage(imgPushbutton[0]);
    }
    
    /**
     * Changes to the image if the positive pin is connected and you click on it to camciar the output pin value.
     */
    public void act() 
    {
        super.act();
        if(pins[1].charge==1&&Greenfoot.mousePressed(this)){
            this.Push();
            pins[0].charge=1;
        }
        else{
            this.UnPush();
            pins[0].charge=0;
        }
    }    
}
